" --------------------------
" symbol_tool
" --------------------------
if type(g:symbol_tool) == type([])
    let s:symbol_tool = join(g:symbol_tool, '-')
    unlet g:symbol_tool
    let g:symbol_tool = s:symbol_tool
    unlet s:symbol_tool
endif
" --------------------------
" set tags
" --------------------------
try
    set tags=./.tags;,.tags
catch /.*/
    let &tags = './.tags;,.tags'
endtry
if get(g:, 'ctags_type', '') != ''
    let g:fzf_tags_command='ctags -R --exclude=.git --exclude=node_modules --exclude=lib --exclude=package-lock.json --exclude=.cache --exclude=.vscode'
endif
" T/F<Cr>
if g:symbol_tool =~ 'leaderftags'
    let g:Lf_Ctags = g:fzf_tags_command
    nnoremap <silent>T<Cr> :LeaderfBufTagAll<Cr>
    nnoremap <silent>F<Cr> :Leaderf function --all<Cr>
    nnoremap <silent><leader>T :LeaderfTag<Cr>
elseif g:symbol_tool =~ 'fzftags'
    nnoremap <silent>T<Cr> :FzfBTags<Cr>
    if executable('perl')
        nnoremap <silent><leader>T :FzfTags<Cr>
    endif
endif
if g:symbol_tool =~ 'leaderftags'
    nnoremap <silent>f<Cr> :Leaderf function<Cr>
elseif Installed('fzf')
    PlugAddOpt 'fzf-funky'
    nnoremap <silent>f<Cr> :FzfFunky<Cr>
elseif Installed('vim-quickui')
    nnoremap <silent>f<Cr> :call quickui#tools#list_function()<Cr>
endif
" t<cr> for tags
if g:complete_engine == 'coc' && UNIX() && g:ctags_type != ''
    nnoremap <silent>t<Cr> :CocFzfList outline<Cr>
elseif g:symbol_tool =~ 'leaderftags'
    nnoremap <silent>t<Cr> :LeaderfBufTag<Cr>
elseif g:symbol_tool =~ 'fzftags'
    if Installed('vista.vim')
        nnoremap <silent>t<Cr> :Vista finder ctags<Cr>
    else
        nnoremap <silent>t<Cr> :FzfBTags<Cr>
    endif
endif
" siderbar tag config
if Installed('tagbar')
    let g:tagbar_position = 'leftabove vertical'
    let g:tagbar_sort  = 0
    let g:tagbar_width = 35
    let g:tagbar_autoclose_netrw = 1
    let g:tagbar_type_css = {
                \ 'ctagstype' : 'css',
                \ 'kinds' : [
                    \ 'c:classes',
                    \ 's:selectors',
                    \ 'i:identities'
                    \ ]}
elseif Installed('vista.vim')
    let g:vista_sidebar_position = 'vertical topleft'
    let g:vista_update_on_text_changed = 1
    let g:vista_sidebar_width = 35
    let g:vista_echo_cursor   = 0
    let g:vista_stay_on_open  = 0
    let g:vista_icon_indent   = ["╰─▸ ", "├─▸ "]
    let g:vista_default_executive = get(g:, 'vista_default_executive', 'ctags')
    let g:vista_executive_for = {
                \ 'vimwiki': 'markdown',
                \ 'pandoc': 'markdown',
                \ 'markdown': 'toc',
                \ }
    if g:ctags_type != ''
        let g:vista_executive_for.go = 'ctags'
    endif
    nnoremap <leader>V :Vista<Space>
endif
" -------------------------------
" vim-gutentags
" -------------------------------
let g:Lf_CacheDirectory = Expand("~/.vim")
let g:gutentags_cache_dir = g:Lf_CacheDirectory . '/LeaderF/gtags'
if !isdirectory(g:gutentags_cache_dir)
    silent! call mkdir(g:gutentags_cache_dir, 'p')
endif
let g:gutentags_modules = []
if Installed('vim-gutentags')
    " exclude files
    let g:gutentags_ctags_exclude = ["*.min.js", "*.min.css", "build", "vendor", "node_modules", "*.vim/bundle/*", ".ccls_cache", "__pycache__"] + g:root_patterns
    " gutentags 搜索工程目录的标志，碰到这些文件/目录名就停止向上一级目录递归
    let g:gutentags_project_root = g:root_patterns
    let g:gutentags_add_default_project_roots = 0
    let g:gutentags_define_advanced_commands = 1
    " 所生成的数据文件的名称
    let g:gutentags_ctags_tagfile = '.tags'
    " modules
    if g:ctags_type != ''
        let g:gutentags_modules += ['ctags']
        " 配置 ctags 的参数
        let g:gutentags_ctags_extra_args = ['--fields=+niazS', '--extras=q', '--c-kinds=+px', '--c++-kinds=+pxl']
        if g:ctags_type =~ "Universal"
            let g:gutentags_ctags_extra_args += ['--output-format=e-ctags']
        endif
    endif
    function! s:delete_tags() abort
        if exists("b:gutentags_files")
            let del_cmds = []
            let col = col('.')
            let line = line('.')
            for [key, path] in items(b:gutentags_files)
                if filereadable(path)
                    if WINDOWS()
                        let cmd = printf('!del %s /a /q' % path)
                    else
                        let cmd = printf('!rm -f %s' % path)
                    endif
                    call add(del_cmds, cmd)
                    let gpath = substitute(path, '-.tags$', '', 'g')
                    if isdirectory(gpath)
                        if WINDOWS()
                            let cmd = printf('!del %s/* /a /q' % gpath)
                        else
                            let cmd = printf('!rm -f %s/*' % gpath)
                        endif
                        call add(del_cmds, cmd)
                    endif
                elseif isdirectory(path)
                    if WINDOWS()
                        let cmd = printf('!del %s/* /a /q' % path)
                    else
                        let cmd = printf('!rm -f %s/*' % path)
                    endif
                    call add(del_cmds, cmd)
                endif
            endfor
            for del_cmd in del_cmds
                silent! exec del_cmd
            endfor
            call cursor(line, col)
        endif
        call lightline#update()
    endfunction
    command! -bar GutentagsDelTags call s:delete_tags()
    command! GutentagsDelTagsAndUpdate GutentagsDelTags | GutentagsUpdate
    nnoremap <leader>gg :GutentagsDelTagsAndUpdate<Cr>
    nnoremap <leader>g<Cr> :GutentagsUpdate<Cr>
    nnoremap <leader>g<Space> :GutentagsDelTags<Cr>
    command! GutentagsCommands call FzfCallCommands('GutentagsCommands', 'Gutentags')
    nnoremap <leader>G :GutentagsCommands<Cr>
endif
" --------------------------
" gtags
" --------------------------
if Installed('gutentags_plus')
    if has('+cscope')
        set cscopeprg=gtags-cscope
    endif
    let g:gutentags_modules += ['gtags_cscope']
    let g:gutentags_define_advanced_commands = 1
    let g:gutentags_auto_add_gtags_cscope = 1
    let g:gutentags_plus_switch = 1
    let g:gutentags_plus_nomap  = 1
    nnoremap <leader>gs <Plug>GscopeFindSymbol
    nnoremap <leader>gd <Plug>GscopeFindDefinition
    nnoremap <leader>gh <Plug>GscopeFindCallingFunc
    nnoremap <leader>gl <Plug>GscopeFindCalledFunc
    nnoremap <leader>gr <Plug>GscopeFindText
    nnoremap <leader>ge <Plug>GscopeFindEgrep
    nnoremap <leader>ga <Plug>GscopeFindAssign
    nnoremap <leader>gt <Plug>GscopeFindCtag
    " file
    nnoremap <leader>gf <Plug>GscopeFindFile
    nnoremap <leader>gi <Plug>GscopeFindInclude
    " kill
    nnoremap <silent><leader>gk :GscopeKill<cr>
elseif has('+cscope')
    set cscopeprg=
endif
source $CFG_PATH/symbol-tag-search.vim
